package com.rzico.sbl.ui.report.driver

import android.widget.TextView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.DriverRecord
import com.rzico.sbl.other.LinearNormalDecoration
import com.rzico.sbl.viewmodel.DriverViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getColorEx
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.helper.*
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.count
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class DriverBarrelRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<DriverViewModel>()

    override fun initData() {
        initTitle(title = "修改记录")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关记录信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            "${(mList[pos] as DriverRecord).letter}月"
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setHeaderHeight(dp2px(40f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<DriverRecord>(R.layout.item_report_station_barrel_record) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_record_time, bean.createDate.getTimeString())
                            text(R.id.item_record_barrel, bean.barrel?.name.orEmpty())
                            text(R.id.item_record_before, bean.beforeBorrow)
                            text(R.id.item_record_after, bean.afterBorrow)
                            text(R.id.item_record_name, bean.admin?.name.orEmpty())

                            with<TextView>(R.id.item_record_count) {
                                val before = bean.beforeBorrow.intValue
                                val after = bean.afterBorrow.intValue
                                val count = after - before

                                it.setTextColor(
                                    getColorEx(
                                        if (count > 0) R.color.black
                                        else R.color.red
                                    )
                                )

                                it.text = when {
                                    count > 0 -> "+$count"
                                    count == 0 -> "-0"
                                    else -> count.toString()
                                }
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    /**
     * 欠桶修改列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .barrelRecordList(
                page = index,
                shopId = intent.getExtra("shopId"),
                barrelId = intent.getExtra("barrelId"),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it.data)
                        if (it.data.count() > 0) pageNum++
                    }

                    mList.onEach {
                        it as DriverRecord
                        it.letter = TimeHelper
                            .getInstance()
                            .getMonthOfDate(
                                it.createDate.getTimeString(),
                                "yyyy-MM-dd HH:mm:ss"
                            )
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}